/*-
 * Copyright (c) 2006, 2007 FTS Team
 *
 * Redistribution and use in source and binary forms, with or without
 * modification, are permitted provided that the following conditions
 * are met:
 * 1. Redistributions of source code must retain the above copyright
 *    notice, this list of conditions and the following disclaimer.
 * 2. Redistributions in binary form must reproduce the above copyright
 *    notice, this list of conditions and the following disclaimer in the
 *    documentation and/or other materials provided with the distribution.
 * 4. Neither the name of the FTS Team nor the names of its contributors
 *    may be used to endorse or promote products derived from this software
 *    without specific prior written permission.
 *
 * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
 * ARE DISCLAIMED.  IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
 * SUCH DAMAGE.
 *
 *      ConfigFileTest.cpp
 *
 * $FTS$
 */
#include <stdio.h>
#include <string.h>
#include <errno.h>
#include <unistd.h>

#include <FTSSException.hpp>
#include <IRConfig.hpp>
#include <XMLParser.hpp>
#include <XMLConfigHandler.hpp>

using namespace FTSS;

#define C_MAX_ZONE_ID_LENGTH 128

int main(int argc, char ** argv)
{

	if (argc != 2) { fprintf(stdout, "usage: %s IRConfigFile.xml\n", argv[0]); return EX_USAGE; }

	IRConfig oConfig(1);
	if (access(argv[1], R_OK) == -1) { fprintf(stderr, "ERROR: Cannot access to file `%s`\n", argv[1]); return EX_SOFTWARE; }
	FILE * F = fopen(argv[1], "rb");
	if (F == NULL) { fprintf(stderr, "ERROR: Cannot open file `%s`: %s\n", argv[1], strerror(errno)); return EX_SOFTWARE; }

	try
	{
		XMLConfigHandler oConfigHandler(oConfig);
		if (XMLParser(&oConfigHandler).ParseFile(F) == -1)
		{
			return EX_SOFTWARE;
		}
	}
	catch(LogicException & e) { fprintf(stderr, "ERROR: %s\n", e.what());  return EX_SOFTWARE; }
	catch(...)                { fprintf(stderr, "ERROR: Unknown Error\n"); return EX_SOFTWARE; }

	fprintf(stdout, "FTSS Configuration reader/checker.\n\n");

	fprintf(stdout, "QueryParser\n");
	fprintf(stdout, "    QuorumSoftness %f\n",   oConfig.GetFloat("QueryParser/QuorumSoftness"));
	fprintf(stdout, "    StopWordWeight %f\n\n", oConfig.GetFloat("QueryParser/StopWordWeight"));

	fprintf(stdout, "Retriever\n");
	fprintf(stdout, "    MaxResults     %d\n",   oConfig.GetInt32("Retriever/MaxResults"));
	fprintf(stdout, "    MinGoodResults %d\n\n", oConfig.GetInt32("Retriever/MinGoodResults"));

	fprintf(stdout, "Ranker\n");
	fprintf(stdout, "    RandomizeW        %f\n",   oConfig.GetFloat("Ranker/RandomizeW"));
	fprintf(stdout, "    DocumentFrameSize %d\n\n", oConfig.GetInt32("Ranker/DocumentFrameSize"));

	UINT_32 iZonesNum = oConfig.GetInt32("Ranker/ZonesNum");
	UINT_32 iZoneId = 0;
	fprintf(stdout, "    Zones\n");

	for (iZoneId = 0; iZoneId < iZonesNum; ++iZoneId)
	{
		CHAR_8 szBuffer[C_MAX_ZONE_ID_LENGTH + 1];
		CHAR_8 szPrintBuffer[C_MAX_ZONE_ID_LENGTH + 1];
		snprintf(szBuffer, C_MAX_ZONE_ID_LENGTH, "Ranker/Zones/PassageZoneW%d", iZoneId);
		snprintf(szPrintBuffer, C_MAX_ZONE_ID_LENGTH, "PassageZoneW%d", iZoneId);
		fprintf(stdout, "        %s: %f\n", szPrintBuffer, oConfig.GetFloat(szBuffer));

		snprintf(szBuffer, C_MAX_ZONE_ID_LENGTH, "Ranker/Zones/TFZoneW%d", iZoneId);
		snprintf(szPrintBuffer, C_MAX_ZONE_ID_LENGTH, "TFZoneW%d", iZoneId);
		fprintf(stdout, "        %s:      %f\n\n", szPrintBuffer, oConfig.GetFloat(szBuffer));
	}

	fprintf(stdout, "    RankTFIDF\n");
	fprintf(stdout, "        K                  %f\n",   oConfig.GetFloat("Ranker/RankTFIDF/K"));
	fprintf(stdout, "        B                  %f\n",   oConfig.GetFloat("Ranker/RankTFIDF/B"));
	fprintf(stdout, "        K1                 %f\n",   oConfig.GetFloat("Ranker/RankTFIDF/K1"));
	fprintf(stdout, "        K2                 %f\n",   oConfig.GetFloat("Ranker/RankTFIDF/K2"));
	fprintf(stdout, "        RankTFIDFWeight    %f\n",   oConfig.GetFloat("Ranker/RankTFIDF/RankTFIDFWeight"));
        fprintf(stdout, "        FrameOverlapRatio  %f\n",   oConfig.GetFloat("Ranker/RankTFIDF/FrameOverlapRatio"));
        fprintf(stdout, "        WordFormTFEQWeight %f\n\n", oConfig.GetFloat("Ranker/RankTFIDF/WordFormTFEQWeight"));

	fprintf(stdout, "    RankPassage\n");
	fprintf(stdout, "        InitialPassageLength %d\n",   oConfig.GetInt32("Ranker/RankPassage/InitialPassageLength"));
	fprintf(stdout, "        PassageLengthDelta   %d\n",   oConfig.GetInt32("Ranker/RankPassage/PassageLengthDelta"));
	fprintf(stdout, "        MaxPassageLength     %d\n",   oConfig.GetInt32("Ranker/RankPassage/MaxPassageLength"));
	fprintf(stdout, "        RankPassageWeight    %f\n",   oConfig.GetFloat("Ranker/RankPassage/RankPassageWeight"));
	fprintf(stdout, "        IncompleteWeight     %f\n",   oConfig.GetFloat("Ranker/RankPassage/IncompleteWeight"));
	fprintf(stdout, "        WordRepeatWeight     %f\n",   oConfig.GetFloat("Ranker/RankPassage/WordRepeatWeight"));
	fprintf(stdout, "        ReverseWeight        %f\n",   oConfig.GetFloat("Ranker/RankPassage/ReverseWeight"));
	fprintf(stdout, "        FrameRankWeight      %f\n\n", oConfig.GetFloat("Ranker/RankPassage/FrameRankWeight"));

	fprintf(stdout, "SnippetCollector\n");
	fprintf(stdout, "    MaxSnippets      %d\n", oConfig.GetInt32("SnippetCollector/MaxSnippets"));
	fprintf(stdout, "    MaxSnippetLength %d\n", oConfig.GetInt32("SnippetCollector/MaxSnippetLength"));
	fprintf(stdout, "    HighlightTag     %s\n\n", oConfig.GetString("SnippetCollector/HighlightTag").c_str());

	fprintf(stdout, "Done.\n");

	// make valgrind happy
	fclose(stdin);
	fclose(stdout);
	fclose(stderr);
return EX_OK;
}
// End.
